<template>
	<view class="uni-container">
		<uni-forms ref="form" v-model="formData" :rules="rules" validateTrigger="bind" @submit="submit">
			<uni-forms-item name="text" label="名称" required>
				<uni-easyinput v-model="formData.text" :clearable="false" placeholder="请输入名称" />
			</uni-forms-item>
			<uni-forms-item name="status" label="是否启用">
				<switch @change="binddata('status', $event.detail.value)" :checked="formData.status" />
			</uni-forms-item>
			<view class="uni-button-group">
				<button style="width: 100px;" type="primary" class="uni-button"
					@click="submitForm">{{$t('common.button.submit')}}</button>
				<navigator open-type="navigateBack" style="margin-left: 15px;"><button style="width: 100px;"
						class="uni-button">{{$t('common.button.back')}}</button></navigator>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	const db = uniCloud.database();
	const dbCmd = db.command;
	const dbCollectionName = 'types';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.includes(key)) {
				result[key] = validator[key]
			}
		}
		return result
	}

	export default {
		data() {
			return {
				formData: {
					"text": "",
					"status": true //默认启用
				},
				rules: {
					"text": {
						"rules": [{
							"format": "bool"
						}]
					},
					"status": {
						"rules": [{
							"format": "bool"
						}]
					}
				},
			}
		},
		onLoad(e) {
			console.log(e)
			const id = e.id
			this.formDataId = id
			this.getDetail(id)
		},
		methods: {
			/**
			 * 跳转应用管理的 list 页
			 */
			gotoAppList() {
				uni.navigateTo({
					url: '../app/list'
				})
			},
			gotoTagList() {
				uni.navigateTo({
					url: '../tag/list'
				})
			},
			gotoTagAdd() {
				uni.navigateTo({
					url: '../tag/add',
					events: {
						refreshCheckboxData: () => {
							this.$refs.checkbox.loadData()
						}
					}
				})
			},

			/**
			 * 触发表单提交
			 */
			submitForm() {
				this.$refs.form.submit();
			},
			/**
			 * 获取表单数据
			 * @param {Object} id
			 */
			getDetail(id) {
				uni.showLoading({
					mask: true
				})
				uniCloud.callFunction({
					name: 'typesadmin',
					data: {
						type: 'detail',
						id: this.formDataId
					},
					success: res => {
						console.log('res', res)
						this.formData = res.result.data[0]
						uni.hideLoading()
					}
				})

			},
			/**
			 * 表单提交
			 * @param {Object} event 回调参数 Function(callback:{value,errors})
			 */
			submit(event) {
				const {
					value,
					errors
				} = event.detail

				// 表单校验失败页面会提示报错 ，要停止表单提交逻辑
				if (errors) {
					return
				}
				uni.showLoading({
					title: '提交中...',
					mask: true
				})
				// 是否启用功能的数据类型转换， 0 正常， 1 禁用
				if (typeof value.status === "boolean") {
					value.status = Number(!value.status)
				}
				this.formData.type = 'add'
				uniCloud.callFunction({
					name: 'update',
					data: {
						dbName: 'types',
						datas: this.formData
					},
					success: res => {
						console.log('res', res)
						if (res.result.status == 0) {
							uni.showToast({
								title: '修改成功！',
							})
							uni.navigateBack({
								delta: 1
							})
						}
					}
				})
			},

		}
	}
</script>
<style>
	::v-deep .uni-forms-item__label {
		width: 90px !important;
	}
</style>